#include "BBGraphicsCache.h"
#include "BBGraphics2D.h"


typedef std::map<const char*,BBGraphics2D*>::iterator graphicsItertorSet;

BBGraphicsCache::BBGraphicsCache( void )
{

}

BBGraphicsCache::~BBGraphicsCache( void )
{

}

BBGraphics2D* BBGraphicsCache::addGraphics( const char* filePath )
{
	BBLog(filePath != NULL,"file is not exist");
	if(filePath == NULL)
		return;
	BBGraphics2D* graphics = BBGraphics2D::createGraphics2D(filePath);
	if(graphics)
		m_pGraphicsList[filePath] = graphics;
	return graphics;
}


BBGraphics2D* BBGraphicsCache::getGraphicsByKey( const char* filePath )
{
	graphicsItertorSet iter;
	for (iter = m_pGraphicsList.begin();iter!=m_pGraphicsList.end();iter++)
	{
		if((*iter).first == filePath)
		{
			return (*iter).second;
		}
	}
	return NULL;
}


void BBGraphicsCache::removeGraphics( BBGraphics2D* graphic )
{
	graphicsItertorSet iter;
	for (iter = m_pGraphicsList.begin();iter!=m_pGraphicsList.end();iter++)
	{
		if((*iter).second == graphic)
		{
			m_pGraphicsList.erase(iter);
			return;
		}
	}
}

void BBGraphicsCache::purgeGraphicCache()
{
	BB_SAFE_DELETE(_mInstance);
}